import { world } from "@minecraft/server";
import { Game, GameComponent } from "@sapi-game/main";
import { EffectIds } from "@sapi-game/utils/vanila-data";
import { WereWolfWolfPhase } from "@/werewolf/phases/wolf";
import { WereWolfStatus } from "@/werewolf/player";

export class WereWolfWolfDaemon extends GameComponent<WereWolfWolfPhase> {
    override onAttach(): void {
        //持续给效果
        this.subscribe(Game.events.interval, () => {
            this.state.getWolves().forEach((p) => {
                p.player?.addEffect(EffectIds.Invisibility, 400, {
                    showParticles: false,
                });
                p.player?.addEffect(EffectIds.Strength, 40, {
                    amplifier: 2,
                    showParticles: false,
                });
            });
        });
        //死人后处理
        this.subscribe(world.afterEvents.playerSpawn, (t) => {
            const p = this.context.players.getById(t.player.id);
            if (p) {
                p.status = WereWolfStatus.killed;
                p.sendMessage("你被杀了，没人救你就寄了");
                const wolves = this.state.getWolves();
                wolves.forEach((pp) => {
                    pp.sendMessage("你们选择杀害:" + p.name);
                    pp.clear("diamond_sword");
                });
                this.state.next();
            }
        });
    }
}
